package com.ttl.bc.ap.stkrequest;

import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;

import com.ttl.bc.ap.stkrequest.BCStkRequestBean;
import com.ttl.bc.ap.stkrequest.BCStkRequestShowBean;
import com.ttl.bc.ap.stkrequest.BCStkRequestSubBean;
import com.ttl.bc.ar.reqconfirm.bean.BCReqConfirmBean;
import com.ttl.bc.ar.reqconfirm.bean.BCReqConfirmSubBean;
import com.ttl.bc.ar.reqconfirm.db.BCReqConfirmDAO;
import com.ttl.bc.master.issuetype.BCIssueTypeBean;
import com.ttl.bc.master.project.BCProjectBean;

import com.ttl.bc.stock.request2.db.BCStkIssue2DAO;
import com.ttl.common.user.department.ITTL_UserDepartment;
import com.ttl.common.util.CommonUtils;
import com.ttl.common.util.Page;

public class BCStkRequestBO {

	@SuppressWarnings("unchecked")
	public boolean add(ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {

		BCStkRequestForm reqForm = setForm(request);//(BCStkIssue2Form)form;
		
		BCIssueTypeBean issueType = new BCIssueTypeBean();
		issueType.setCode(request.getParameter("issueType"));			
		
		BCProjectBean proj = new BCProjectBean();
		proj.setCode(reqForm.getProjectCode());
		
		BCStkRequestBean  stkReq = setRequestObject(reqForm);
		stkReq.setIttl_userLevel((Integer)request.getSession().getAttribute("userLevel"));
		stkReq.setIttl_status("0");
		
		BCStkRequestDAO dao = new BCStkRequestDAO();
		String hql = "FROM BCStkRequestBean WHERE docNo = '"+stkReq.getDocNo()+"' ";
		List al = dao.find(hql);
		if(al!=null && al.size()>0) {
			String maxDocNo = CommonUtils.getMaxStkRequestDocNo(stkReq.getDocNo().substring(0, 5));
			stkReq.setDocNo(maxDocNo);
			dao.add(stkReq);			
			
		}else {
			dao.add(stkReq);
		}

		String user 	 = (String)request.getSession().getAttribute("userName");
		System.out.println(CommonUtils.getDateTimeForStamp()+" #user:"+user+" #use :BCStkRequestDAO #mode :add");
		
		BCReqConfirmBean reqConfirm = setReqConfirm(stkReq);
		reqConfirm.setDocNo(stkReq.getDocNo());
		reqConfirm.setIttl_userLevel((Integer)request.getSession().getAttribute("userLevel"));
		reqConfirm.setIttl_status("0");
		
		BCReqConfirmDAO dao2 = new BCReqConfirmDAO();
		dao2.add(reqConfirm);
		
		request.setAttribute("docNoPrint", stkReq.getDocNo());
		System.out.println(CommonUtils.getDateTimeForStamp()+" #user:"+user+" #use :BCReqConfirmDAO #mode :add");
		
		return true;
	}

	@SuppressWarnings("unchecked")
	public static BCReqConfirmBean setReqConfirm(BCStkRequestBean stkReq) {
		BCReqConfirmBean cf = new BCReqConfirmBean();
		
		cf.setDocNo(stkReq.getDocNo());
		cf.setDocDate(stkReq.getDocDate());
		cf.setCreatorCode(stkReq.getCreatorCode());
		cf.setCreateDateTime(stkReq.getCreateDateTime());
		cf.setIsCancel(0);
		cf.setIsConfirm(0);
		cf.setBillStatus(0);
		cf.setConfirmName(stkReq.getCreatorCode());
		cf.setMyDescription(stkReq.getMyDescription());
		
		HashSet h = new HashSet();
    	Iterator it = stkReq.getBcStkRequestSubs().iterator();
    	
        while(it.hasNext()) {
        	BCStkRequestSubBean sub = (BCStkRequestSubBean)it.next();
        	
        	BCReqConfirmSubBean cfSub = new BCReqConfirmSubBean();
        	cfSub.setIttl_status("0");
        	cfSub.setDocDate(sub.getDocDate());
        	cfSub.setItemCode(sub.getItemCode());
        	cfSub.setStkRequestNo(stkReq.getDocNo());
        	cfSub.setDepartCode(sub.getDepartCode());
        	cfSub.setQty(sub.getQty());
        	cfSub.setConfirmQty(sub.getQty());
        	cfSub.setRemainQty(sub.getRemainQty());
//        	cfSub.setUnitCode(sub.getBcitemUnit().getCode());
        	cfSub.setUnitCode(sub.getUnitCode());
        	cfSub.setLineNumber(sub.getLineNumber());
        	cfSub.setItemName(sub.getItemName());
        	cfSub.setCreatorCode(sub.getCreatorCode());
        	cfSub.setCreateDateTime(sub.getCreateDateTime());
        	cfSub.setIttl_unitName(sub.getIttl_unitName());
        	cfSub.setBcReqConfirm(cf);
        	
        	cfSub.setPrice(sub.getPrice());
        	cfSub.setAmount(sub.getAmount());
        	cfSub.setMyDescription(sub.getMyDescription());
        	h.add(cfSub);
        }
        cf.setBcReqConfirmSubs(h);
        
        return cf;
				
	}

	@SuppressWarnings("unchecked")
	public static BCStkRequestBean setRequestObject(BCStkRequestForm reqForm) {
		
		BCStkRequestBean issue2 = new BCStkRequestBean();
						
		issue2.setBillStatus(1);
		issue2.setIsConfirm(1);
		issue2.setIsCancel(0);
				
		issue2.setDocNo(reqForm.getDocNo());
		issue2.setCreatorCode(reqForm.getCreatorCode());
		issue2.setDepartCode(reqForm.getDepartCode());
					
	    issue2.setDocDate(CommonUtils.convertStringToDate(reqForm.getDocDate()));
	    //System.out.println("issue2.getDocDate()="+issue2.getDocDate());
	    issue2.setCreateDateTime(new Date());
		
		issue2.setIttl_deptRequest(reqForm.getiTTL_deptRequest());
		issue2.setProjectCode(reqForm.getProjectCode());		
		
		issue2.setMyDescription(reqForm.getMyDescription());
				
		HashSet s = new HashSet();			
				
		if( reqForm.getItemCode1() != null) {
			BCStkRequestSubBean issueSub2 = new BCStkRequestSubBean();
			
			issueSub2.setIttl_status("0");
			issueSub2.setDepartCode(reqForm.getDepartCode());
			issueSub2.setDocDate(issue2.getDocDate());							
			issueSub2.setItemCode(reqForm.getItemCode1());
			issueSub2.setItemName(reqForm.getItemName1());
			
			issueSub2.setIttl_itemCode_ttl2(reqForm.getShortName1());
			
			issueSub2.setQty(Float.valueOf(reqForm.getQty1()));
			issueSub2.setRemainQty(Float.valueOf(reqForm.getQty1()));
					
			issueSub2.setPrice(Float.valueOf(reqForm.getPrice1()));
			issueSub2.setAmount(Float.valueOf(reqForm.getAmount1()));
			
/*			BCItemUnitBean itemU = new BCItemUnitBean();
			itemU.setCode(reqForm.getUnitCode1());
			issueSub2.setBcitemUnit(itemU);*/
			issueSub2.setUnitCode(reqForm.getUnitCode1());
			
			issueSub2.setLineNumber(1);
			issueSub2.setIttl_unitName(reqForm.getUnitName1());
			
//			System.out.println("##################### 1");
//			System.out.println(issueSub2.getIttl_unitName());
//			System.out.println("#####################");
			
			issueSub2.setIttl_division(reqForm.getDivision1());								
			
			issueSub2.setProjectCode(reqForm.getProjectCode());
			issueSub2.setCreatorCode(reqForm.getCreatorCode());
			issueSub2.setCreateDateTime(issue2.getCreateDateTime());
			issueSub2.setMyDescription(reqForm.getMyDescription1());
			issueSub2.setBcStkRequest(issue2);
						
			s.add(issueSub2);
		}
		
		if(reqForm.getItemCode2() != null) {
			BCStkRequestSubBean issueSub2 = new BCStkRequestSubBean();
			issueSub2.setIttl_status("0");
			issueSub2.setDepartCode(reqForm.getDepartCode());
			issueSub2.setDocDate(issue2.getDocDate());							
			issueSub2.setItemCode(reqForm.getItemCode2());
			issueSub2.setItemName(reqForm.getItemName2());
			issueSub2.setIttl_itemCode_ttl2(reqForm.getShortName2());
			issueSub2.setQty(Float.valueOf(reqForm.getQty2()));
			issueSub2.setRemainQty(Float.valueOf(reqForm.getQty2()));

			issueSub2.setPrice(Float.valueOf(reqForm.getPrice2()));
			issueSub2.setAmount(Float.valueOf(reqForm.getAmount2()));
			
/*			BCItemUnitBean itemU = new BCItemUnitBean();
			itemU.setCode(reqForm.getUnitCode2());
			issueSub2.setBcitemUnit(itemU);*/	
			issueSub2.setUnitCode(reqForm.getUnitCode2());
			issueSub2.setLineNumber(2);
			issueSub2.setIttl_unitName(reqForm.getUnitName2());
//			System.out.println("##################### 2");
//			System.out.println(issueSub2.getIttl_unitName());
//			System.out.println("#####################");
			
			issueSub2.setIttl_division(reqForm.getDivision2());								
			
			issueSub2.setProjectCode(reqForm.getProjectCode());
			issueSub2.setCreatorCode(reqForm.getCreatorCode());
			issueSub2.setCreateDateTime(issue2.getCreateDateTime());
			issueSub2.setMyDescription(reqForm.getMyDescription2());
			issueSub2.setBcStkRequest(issue2);
						
			s.add(issueSub2);
		}		
		
		if( reqForm.getItemCode3() != null) {
			BCStkRequestSubBean issueSub2 = new BCStkRequestSubBean();
			issueSub2.setIttl_status("0");
			issueSub2.setDepartCode(reqForm.getDepartCode());
			issueSub2.setDocDate(issue2.getDocDate());							
			issueSub2.setItemCode(reqForm.getItemCode3());
			issueSub2.setItemName(reqForm.getItemName3());
			issueSub2.setIttl_itemCode_ttl2(reqForm.getShortName3());
			issueSub2.setQty(Float.valueOf(reqForm.getQty3()));
			issueSub2.setRemainQty(Float.valueOf(reqForm.getQty3()));
			
			issueSub2.setPrice(Float.valueOf(reqForm.getPrice3()));
			issueSub2.setAmount(Float.valueOf(reqForm.getAmount3()));
			
/*			BCItemUnitBean itemU = new BCItemUnitBean();
			itemU.setCode(reqForm.getUnitCode3());
			issueSub2.setBcitemUnit(itemU);*/
			issueSub2.setUnitCode(reqForm.getUnitCode3());
			issueSub2.setLineNumber(3);
			issueSub2.setIttl_unitName(reqForm.getUnitName3());
//			System.out.println("##################### 3");
//			System.out.println(issueSub2.getIttl_unitName());
//			System.out.println("#####################");
			
			issueSub2.setIttl_division(reqForm.getDivision3());								
			
			issueSub2.setProjectCode(reqForm.getProjectCode());
			issueSub2.setCreatorCode(reqForm.getCreatorCode());
			issueSub2.setCreateDateTime(issue2.getCreateDateTime());
			issueSub2.setMyDescription(reqForm.getMyDescription3());
			issueSub2.setBcStkRequest(issue2);
						
			s.add(issueSub2);
		}		
		
		if(reqForm.getItemCode4() != null) {
			BCStkRequestSubBean issueSub2 = new BCStkRequestSubBean();
			issueSub2.setIttl_status("0");
			issueSub2.setDepartCode(reqForm.getDepartCode());
			issueSub2.setDocDate(issue2.getDocDate());							
			issueSub2.setItemCode(reqForm.getItemCode4());
			issueSub2.setItemName(reqForm.getItemName4());
			issueSub2.setIttl_itemCode_ttl2(reqForm.getShortName4());
			issueSub2.setQty(Float.valueOf(reqForm.getQty4()));
			issueSub2.setRemainQty(Float.valueOf(reqForm.getQty4()));
			
			issueSub2.setPrice(Float.valueOf(reqForm.getPrice4()));
			issueSub2.setAmount(Float.valueOf(reqForm.getAmount4()));
			
/*			BCItemUnitBean itemU = new BCItemUnitBean();
			itemU.setCode(reqForm.getUnitCode4());
			issueSub2.setBcitemUnit(itemU);*/
			issueSub2.setUnitCode(reqForm.getUnitCode4());
			issueSub2.setLineNumber(4);
			issueSub2.setIttl_unitName(reqForm.getUnitName4());
//			System.out.println("##################### 4");
//			System.out.println(issueSub2.getIttl_unitName());
//			System.out.println("#####################");
			
			issueSub2.setIttl_division(reqForm.getDivision4());								
			
			issueSub2.setProjectCode(reqForm.getProjectCode());
			issueSub2.setCreatorCode(reqForm.getCreatorCode());
			issueSub2.setCreateDateTime(issue2.getCreateDateTime());
			issueSub2.setMyDescription(reqForm.getMyDescription4());
			issueSub2.setBcStkRequest(issue2);
						
			s.add(issueSub2);
		}
					
		issue2.setBcStkRequestSubs(s);
		return issue2;
	}

	public static BCStkRequestForm setForm(HttpServletRequest request) {
		
		BCStkRequestForm reqForm = new BCStkRequestForm();
				
		reqForm.setDocNo(request.getParameter("docNo"));
		reqForm.setDocDate(request.getParameter("docDate"));
		//System.out.println("reqForm.getDocDate()="+reqForm.getDocDate());
		reqForm.setDepartCode(request.getParameter("departCode"))	;		
		reqForm.setProjectCode(request.getParameter("projectCode"));
		reqForm.setCreatorCode(request.getParameter("creatorCode"));		
		reqForm.setiTTL_deptRequest(request.getParameter("iTTL_deptRequest"));
		
		try {			
			reqForm.setMyDescription(new String((""+request.getParameter("myDescription")).getBytes("ISO8859_1"),"UTF-8"));
		} catch(Exception ex){}
		
		try {
			reqForm.setItemName1(new String((""+request.getParameter("itemName1")).getBytes("ISO8859_1"),"UTF-8"));
			reqForm.setUnitCode1(new String((""+request.getParameter("unitCode1")).getBytes("ISO8859_1"),"UTF-8"));
			reqForm.setUnitName1(new String((""+request.getParameter("unitName1")).getBytes("ISO8859_1"),"UTF-8"));
			reqForm.setDivision1(new String((""+request.getParameter("division1")).getBytes("ISO8859_1"),"UTF-8"));
			reqForm.setMyDescription1(new String((""+request.getParameter("myDescription1")).getBytes("ISO8859_1"),"UTF-8"));
			//System.out.println("reqForm.getMyDescription1()="+reqForm.getMyDescription1());
		} catch(Exception ex){}
		try {
			reqForm.setItemName2(new String((""+request.getParameter("itemName2")).getBytes("ISO8859_1"),"UTF-8"));
			reqForm.setUnitCode2(new String((""+request.getParameter("unitCode2")).getBytes("ISO8859_1"),"UTF-8"));
			reqForm.setUnitName2(new String((""+request.getParameter("unitName2")).getBytes("ISO8859_1"),"UTF-8"));
			reqForm.setDivision2(new String((""+request.getParameter("division2")).getBytes("ISO8859_1"),"UTF-8"));
			reqForm.setMyDescription2(new String((""+request.getParameter("myDescription2")).getBytes("ISO8859_1"),"UTF-8"));
			//System.out.println("reqForm.getMyDescription2()="+reqForm.getMyDescription2());
		} catch(Exception ex){}
		try {			
			reqForm.setItemName3(new String((""+request.getParameter("itemName3")).getBytes("ISO8859_1"),"UTF-8"));
			reqForm.setUnitCode3(new String((""+request.getParameter("unitCode3")).getBytes("ISO8859_1"),"UTF-8"));
			reqForm.setUnitName3(new String((""+request.getParameter("unitName3")).getBytes("ISO8859_1"),"UTF-8"));
			reqForm.setDivision3(new String((""+request.getParameter("division3")).getBytes("ISO8859_1"),"UTF-8"));
			reqForm.setMyDescription3(new String((""+request.getParameter("myDescription3")).getBytes("ISO8859_1"),"UTF-8"));
			//System.out.println("reqForm.getMyDescription3()="+reqForm.getMyDescription3());
		} catch(Exception ex){}
		try {			
			reqForm.setItemName4(new String((""+request.getParameter("itemName4")).getBytes("ISO8859_1"),"UTF-8"));
			reqForm.setUnitCode4(new String((""+request.getParameter("unitCode4")).getBytes("ISO8859_1"),"UTF-8"));
			reqForm.setUnitName4(new String((""+request.getParameter("unitName4")).getBytes("ISO8859_1"),"UTF-8"));
			reqForm.setDivision4(new String((""+request.getParameter("division4")).getBytes("ISO8859_1"),"UTF-8"));
			reqForm.setMyDescription4(new String((""+request.getParameter("myDescription4")).getBytes("ISO8859_1"),"UTF-8"));
			//System.out.println("reqForm.getMyDescription4()="+reqForm.getMyDescription4());
		}catch (Exception e) {
			//System.out.println("############ error: "+e.toString());
		}															
				
		reqForm.setRowOrder1(request.getParameter("rowOrder1"));		
		reqForm.setItemCode1(request.getParameter("itemCode1"));			
		reqForm.setShortName1(request.getParameter("shortName1"));						
		reqForm.setQty1(request.getParameter("qty1"));				
		reqForm.setPrice1(request.getParameter("price1"));
		reqForm.setAmount1(request.getParameter("amount1"));
		System.out.println("rowOrder1 ="+reqForm.getRowOrder1());
		
		reqForm.setRowOrder2(request.getParameter("rowOrder2"));
		reqForm.setItemCode2(request.getParameter("itemCode2"));
		reqForm.setShortName2(request.getParameter("shortName2"));
		reqForm.setQty2(request.getParameter("qty2"));				
		reqForm.setPrice2(request.getParameter("price2"));
		reqForm.setAmount2(request.getParameter("amount2"));
		System.out.println("rowOrder2 ="+reqForm.getRowOrder2());
		
		reqForm.setRowOrder3(request.getParameter("rowOrder3"));
		reqForm.setItemCode3(request.getParameter("itemCode3"));
		reqForm.setShortName3(request.getParameter("shortName3"));
		reqForm.setQty3(request.getParameter("qty3"));				
		reqForm.setPrice3(request.getParameter("price3"));
		reqForm.setAmount3(request.getParameter("amount3"));
		System.out.println("rowOrder3 ="+reqForm.getRowOrder3());
		
		reqForm.setRowOrder4(request.getParameter("rowOrder4"));
		reqForm.setItemCode4(request.getParameter("itemCode4"));
		reqForm.setShortName4(request.getParameter("shortName4"));
		reqForm.setQty4(request.getParameter("qty4"));													
		reqForm.setPrice4(request.getParameter("price4"));
		reqForm.setAmount4(request.getParameter("amount4"));
		System.out.println("rowOrder4 ="+reqForm.getRowOrder4());
		
		return reqForm;
		
	}

	@SuppressWarnings("unchecked")
	public List find(HttpServletRequest request) {
		
		String userName = (String)request.getSession().getAttribute("userName");			
		String monthFind = (String)request.getParameter("monthFind");
		String yearFind = (String)request.getParameter("yearFind");

		String docNoFind = (String)request.getParameter("docNoFind");
		String preDocNo  = (String)request.getSession().getAttribute("preDocNo")+"-PR";
		int userLevel =  (Integer)request.getSession().getAttribute("userLevel");		
		
		if(monthFind==null) {			
			monthFind = CommonUtils.getCurrentDateFormat("MM");										
		}
		
		if(yearFind==null) {
			yearFind = CommonUtils.getCurrentDateFormat("yyyyy");
			
			if (Integer.parseInt(yearFind)<2500) {								
				yearFind = (""+(Integer.parseInt(yearFind)+543)).substring(2);			
			}else {
				yearFind = yearFind.substring(2);
			}			
		}
				
		request.getSession().setAttribute("monthFind", monthFind);
		request.getSession().setAttribute("yearFind", yearFind);
		
		String hql = "";
		if(docNoFind==null||docNoFind.trim().equals("")) {
			hql = "FROM BCStkRequestBean s  " +
					 "WHERE upper(s.creatorCode)='"+userName.toUpperCase()+"' " +
					 "and s.docNo like '"+preDocNo+"-"+yearFind+monthFind+"%' "+
//					 "and s.isCancel =0 "+
					 "ORDER BY s.docNo desc";			
		}else {
			hql = "FROM BCStkRequestBean s  " +
					 "WHERE upper(s.creatorCode)='"+userName.toUpperCase()+"' " +
					 "and s.docNo like '"+preDocNo+"-%"+docNoFind.trim()+"%' "+
//					 "and s.isCancel =0 "+
					 "ORDER BY s.docNo desc";
			request.getSession().setAttribute("docNoFind", docNoFind.trim());
		}
		
		System.out.println(hql);
		BCStkRequestDAO dao = new BCStkRequestDAO();
		List results = dao.find(hql);

		//สร้าง ArrayList หลักไว้สำหรับ รวมกันของผล 2 hql
		ArrayList<BCStkRequestShowBean> alUnion = new ArrayList<BCStkRequestShowBean>();
		String docNoNotIn = "";		
		
		if(results!=null) {			
			ArrayList<BCStkRequestShowBean> al = getBCStkRequestShowBean(results);
			System.out.println("size1:"+results.size());
			for(int i=0; i<al.size(); i++) {
				alUnion.add(al.get(i));
				docNoNotIn = docNoNotIn + "'"+((BCStkRequestShowBean)al.get(i)).getDocNo()+"',"; 
			}		
		}
//		System.out.println("docNoNotIn = "+docNoNotIn);
		//สำหรับ userLevel
		if(docNoFind==null||docNoFind.trim().equals("")) {
			hql = "FROM BCStkRequestBean s  " +
			 "WHERE "+
//			 " and s.isCancel =0 "+
			 " s.ittl_userLevel <= "+userLevel;
			 if(docNoNotIn.length()>0){
				 docNoNotIn  =docNoNotIn.substring(0, docNoNotIn.length()-1);
				 hql = hql+" and s.docNo not in ("+docNoNotIn+") ";
			 }
			 if(userLevel==7){
				 hql = hql+" and s.docNo like '%"+yearFind+monthFind+"%' ";
			 }else {
				 hql = hql+" and s.docNo like '"+preDocNo+"-"+yearFind+monthFind+"%' ";
			 }
			 
			 hql = hql+" ORDER BY s.docNo desc";			
		}else {
			hql = "FROM BCStkRequestBean s  " +
			 " WHERE  "+
//			 " and s.isCancel =0 "+
			 " s.ittl_userLevel <="+userLevel;
			 if(docNoNotIn.length()>0){
				 docNoNotIn  =docNoNotIn.substring(0, docNoNotIn.length()-1);
				 hql = hql+" and s.docNo not in ("+docNoNotIn+") ";
			 }
			 if(userLevel==7){
				 hql = hql+" and s.docNo like '%"+docNoFind.trim()+"%' ";
			 }else {
				 hql = hql+" and s.docNo like '"+preDocNo+"-%"+docNoFind.trim()+"%' ";
			 }			 
			 hql = hql+" ORDER BY s.docNo desc";				
		}
		
		List results2 = dao.find(hql);
		if(results2!=null) {					
			ArrayList<BCStkRequestShowBean> al = getBCStkRequestShowBean(results2);
			System.out.println("size2:"+results2.size());
			for(int i=0; i<al.size(); i++) {
				alUnion.add(al.get(i));
			}
		}										
		results = alUnion;		
		
		return results;
	}
	
	@SuppressWarnings("unchecked")
	private ArrayList<BCStkRequestShowBean> getBCStkRequestShowBean(List results) {
		ArrayList<BCStkRequestShowBean> al = new ArrayList<BCStkRequestShowBean>();
		
		for(int i=0; i<results.size(); i++) {
			BCStkRequestBean s2 = (BCStkRequestBean)results.get(i);
			
			for(Object o: s2.getBcStkRequestSubs()) {
				BCStkRequestSubBean sub2 = (BCStkRequestSubBean)o; 			
				
				BCStkRequestShowBean bean = new BCStkRequestShowBean();
				bean.setDocNo(s2.getDocNo());
				bean.setDocDate(s2.getDocDate());
				bean.setMyDescription(s2.getMyDescription());
				bean.setBillStatus(s2.getBillStatus());					
				bean.setItemCode(sub2.getItemCode());
				bean.setItemName(sub2.getItemName());					
				bean.setQty(sub2.getQty());					
				bean.setUnitName(sub2.getIttl_unitName());
				bean.setPrice(sub2.getPrice());
				bean.setAmount(sub2.getAmount());
				bean.setIttl_status(sub2.getIttl_status());
				bean.setIttl_status_head(s2.getIttl_status());
				bean.setMode("");								
				
				al.add(bean);
			}				
		}
		return al;
	}

	@SuppressWarnings("unchecked")
	public List findAll(HttpServletRequest request) {
		
		@SuppressWarnings("unused")
		String userName = (String)request.getSession().getAttribute("userName");			
		String monthFind = (String)request.getParameter("monthFind");
		String yearFind = (String)request.getParameter("yearFind");
		
		if(monthFind==null || yearFind==null) {
			Date date = new Date();
			SimpleDateFormat sdf = new SimpleDateFormat("MM");			
			monthFind = sdf.format(date);
			
			sdf = new SimpleDateFormat("yyyy");
			String yy="";
			if (Integer.parseInt(sdf.format(date))<2500) {
				yy = (""+Integer.parseInt(sdf.format(date))+543).substring(2,2);
			}else {
				yy = sdf.format(date).substring(2,2);
			}			
			yearFind = yy;			
		}
				
		String hql = "FROM BCStkRequestBean s  " +
					 "WHERE s.isConfirm =0 " +
					 "and s.docNo like '%"+yearFind+monthFind+"%' "+
					 "and s.isCancel =0 "+
					 "and s.billStatus =0 "+
					 "ORDER BY s.docNo desc";								
		
		BCStkRequestDAO dao = new BCStkRequestDAO();
		List results = dao.find(hql);

		if(results.size()>0) {			
			ArrayList<BCStkRequestShowBean> al = new ArrayList<BCStkRequestShowBean>();
			
			for(int i=0; i<results.size(); i++) {
				BCStkRequestBean s2 = (BCStkRequestBean)results.get(i);
				
				for(Object o: s2.getBcStkRequestSubs()) {
					BCStkRequestSubBean sub2 = (BCStkRequestSubBean)o; 			
					
					BCStkRequestShowBean bean = new BCStkRequestShowBean();
					bean.setDocNo(s2.getDocNo());
					bean.setDocDate(s2.getDocDate());
					bean.setMyDescription(s2.getMyDescription());
					bean.setBillStatus(s2.getBillStatus());					
					bean.setItemCode(sub2.getItemCode());
					bean.setItemName(sub2.getItemName());					
					bean.setQty(sub2.getQty());					
					bean.setIttl_unitName(sub2.getIttl_unitName());
					bean.setPrice(sub2.getPrice());
					bean.setAmount(sub2.getAmount());
					bean.setMode("");
					
					al.add(bean);
				}				
			}
			results = al;
		}
		
		return results;
	}	

	@SuppressWarnings("unchecked")
	public ArrayList findPrint(String hql) {
		BCStkIssue2DAO dao = new BCStkIssue2DAO();
		ArrayList results = (ArrayList)dao.find(hql);
		return results;	
	}

	public boolean delete(ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		
		String docNo = (String)request.getParameter("docNo");
		String hql = "from BCStkRequestBean where docNo = '"+docNo+"'";
//		hql = hql + " and billStatus=1 and isCancel =0 ";
		
		System.out.println("#docNo :"+docNo);
		
		BCStkRequestDAO dao = new BCStkRequestDAO();
		dao.delete(hql);
		
		String hql2 = "from BCReqConfirmBean where docNo = '"+docNo+"'";
//		hql2 = hql2 + " and billStatus=0 and isCancel =0 ";		
		BCReqConfirmDAO dao2 = new BCReqConfirmDAO();
		dao2.delete(hql2);
		
		request.setAttribute("docNoFind", docNo.substring(9, docNo.length()));
		
		return true;
	}

	@SuppressWarnings("unchecked")
	public Object findForm(HttpServletRequest request) {
				
		BCStkRequestForm form = new BCStkRequestForm();
		
		String docNo = (String)request.getParameter("docNo");
		System.out.println("#docNo :"+docNo);
		
		BCStkRequestDAO dao = new BCStkRequestDAO();	
		String hql = "FROM BCStkRequestBean h " +
				" WHERE upper(h.docNo) = '"+docNo.toUpperCase()+"' ";
				
		List results = dao.find(hql);
		if (results!=null && results.size()>0) {
			
			for(int i=0; i<results.size(); i++) {				
				BCStkRequestBean s2 = (BCStkRequestBean)results.get(i);
								
				form.setDocNo(docNo);
				//System.out.println("s2.getDocDate()="+s2.getDocDate());
				form.setDocDate(""+s2.getDocDate());
				//System.out.println("form.getDocDate()="+form.getDocDate());
				form.setDepartCode(s2.getDepartCode());
				form.setProjectCode(s2.getProjectCode());
				form.setCreatorCode(s2.getCreatorCode());
				form.setiTTL_deptRequest(s2.getIttl_deptRequest());
				form.setMyDescription(s2.getMyDescription());
																																
				int id = 0;
				int countId = 0;
				for(Object o: s2.getBcStkRequestSubs()) {
					countId++;
					BCStkRequestSubBean sub2 = (BCStkRequestSubBean)o;
					
					if(sub2.getIttl_status().equals("0") ||
							sub2.getIttl_status().equals("-1") ) {
						id = sub2.getLineNumber();					
						switch (id) {
						case 1:	
							form.setRowOrder1(""+sub2.getRowOrder());
							form.setItemCode1(sub2.getItemCode());
							form.setItemName1(sub2.getItemName());
							form.setShortName1(sub2.getIttl_itemCode_ttl2());
/*							form.setUnitCode1(sub2.getBcitemUnit().getCode());
							form.setUnitName1(sub2.getBcitemUnit().getName());*/
							form.setUnitCode1(sub2.getUnitCode());
							form.setUnitName1(sub2.getIttl_unitName());
							
							form.setQty1(""+sub2.getQty());						
							form.setDivision1(sub2.getIttl_division());						
							form.setIttl_unitName1(sub2.getIttl_unitName());
							form.setPrice1(""+sub2.getPrice());
							form.setAmount1(""+sub2.getAmount());
							form.setMyDescription1(sub2.getMyDescription());
							break;
						case 2:
							form.setRowOrder2(""+sub2.getRowOrder());
							form.setItemCode2(sub2.getItemCode());
							form.setItemName2(sub2.getItemName());
							form.setShortName2(sub2.getIttl_itemCode_ttl2());
//							form.setUnitCode2(sub2.getBcitemUnit().getCode());
//							form.setUnitName2(sub2.getBcitemUnit().getName());
							form.setUnitCode2(sub2.getUnitCode());
							form.setUnitName2(sub2.getIttl_unitName());
							
							form.setQty2(""+sub2.getQty());						
							form.setDivision2(sub2.getIttl_division());
							form.setIttl_unitName2(sub2.getIttl_unitName());
							form.setPrice2(""+sub2.getPrice());
							form.setAmount2(""+sub2.getAmount());
							form.setMyDescription2(sub2.getMyDescription());
							break;
						case 3:
							form.setRowOrder3(""+sub2.getRowOrder());
							form.setItemCode3(sub2.getItemCode());
							form.setItemName3(sub2.getItemName());
							form.setShortName3(sub2.getIttl_itemCode_ttl2());
//							form.setUnitCode3(sub2.getBcitemUnit().getCode());
//							form.setUnitName3(sub2.getBcitemUnit().getName());
							form.setUnitCode3(sub2.getUnitCode());
							form.setUnitName3(sub2.getIttl_unitName());
							
							form.setQty3(""+sub2.getQty());						
							form.setDivision3(sub2.getIttl_division());
							form.setIttl_unitName3(sub2.getIttl_unitName());
							form.setPrice3(""+sub2.getPrice());
							form.setAmount3(""+sub2.getAmount());
							form.setMyDescription3(sub2.getMyDescription());
							break;		
						case 4:
							form.setRowOrder4(""+sub2.getRowOrder());
							form.setItemCode4(sub2.getItemCode());
							form.setItemName4(sub2.getItemName());
							form.setShortName4(sub2.getIttl_itemCode_ttl2());
//							form.setUnitCode4(sub2.getBcitemUnit().getCode());
//							form.setUnitName4(sub2.getBcitemUnit().getName());
							form.setUnitCode4(sub2.getUnitCode());
							form.setUnitName4(sub2.getIttl_unitName());		
							
							form.setQty4(""+sub2.getQty());						
							form.setDivision4(sub2.getIttl_division());
							form.setIttl_unitName4(sub2.getIttl_unitName());
							form.setPrice4(""+sub2.getPrice());
							form.setAmount4(""+sub2.getAmount());
							form.setMyDescription4(sub2.getMyDescription());
							break;						
						}						
					}					
				}
				request.setAttribute("countId", countId);
			}			
		}	
		return form;
	}

	public boolean update(ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {

		BCStkRequestForm reqForm = setForm(request);				
		BCStkRequestBean  stkReq = setRequestObject(reqForm);		
		stkReq.setIttl_status("0");
		stkReq.setIttl_userLevel((Integer)request.getSession().getAttribute("userLevel"));
		
		BCStkRequestDAO dao = new BCStkRequestDAO();
		dao.update(stkReq.getDocNo(),stkReq);
		
		String user 	 = (String)request.getSession().getAttribute("userName");
		System.out.println(CommonUtils.getDateTimeForStamp()+" #user:"+user+" #use :BCStkRequestDAO #mode :update");
		
		BCReqConfirmBean reqComfirm = setReqConfirm(stkReq);						
		reqComfirm.setIttl_status("0");
		reqComfirm.setIttl_userLevel((Integer)request.getSession().getAttribute("userLevel"));
		
		BCReqConfirmDAO dao2 = new BCReqConfirmDAO();
		dao2.update(reqComfirm.getDocNo(), reqComfirm);
				
		System.out.println(CommonUtils.getDateTimeForStamp()+" #user:"+user+" #use :BCReqConfirmDAO #mode :update");	
		
		//request.getSession().setAttribute("itemFind", reqForm.getDocNo().substring(9, reqForm.getDocNo().length()));
		request.getSession().setAttribute("itemFind", reqForm.getDocNo());
		return true;
										
	}

	@SuppressWarnings("unchecked")
	public boolean updatePrice(ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		
		BCStkRequestForm reqForm = setForm(request);				
		BCStkRequestBean  stkReq = setRequestObject(reqForm);
		stkReq.setIttl_status("3");
		stkReq.setIttl_userLevel((Integer)request.getSession().getAttribute("userLevel"));
		
		Iterator it = stkReq.getBcStkRequestSubs().iterator();
		while(it.hasNext()) {
			BCStkRequestSubBean sub = (BCStkRequestSubBean)it.next();
			sub.setIttl_status("3");
		}
		
									
		BCStkRequestDAO dao = new BCStkRequestDAO();
		dao.update(stkReq.getDocNo(),stkReq);
		
		String user 	 = (String)request.getSession().getAttribute("userName");
		System.out.println(CommonUtils.getDateTimeForStamp()+" #user:"+user+" #use :BCStkRequestDAO #mode :updatePrice");
		
		BCReqConfirmBean reqConfirm = setReqConfirm(stkReq);		
		reqConfirm.setIttl_status("3");
		reqConfirm.setIttl_userLevel((Integer)request.getSession().getAttribute("userLevel"));
		
		it = reqConfirm.getBcReqConfirmSubs().iterator();
		while(it.hasNext()) {
			BCReqConfirmSubBean sub = (BCReqConfirmSubBean)it.next();
			sub.setIttl_status("3");
		}
		
		BCReqConfirmDAO dao2 = new BCReqConfirmDAO();
		dao2.update(reqConfirm.getDocNo(), reqConfirm);
				
		System.out.println(CommonUtils.getDateTimeForStamp()+" #user:"+user+" #use :BCReqConfirmDAO #mode :updatePrice");	
		request.setAttribute("docNoFind", reqForm.getDocNo().substring(9, reqForm.getDocNo().length()));
		
		return true;
	}

	public boolean restore(ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		
		String docNo = (String)request.getParameter("docNo");
		String hql = "from BCStkRequestBean where docNo = '"+docNo+"'";
//		hql = hql + " and billStatus=1 and isCancel =0 ";
		
		System.out.println("#docNo :"+docNo);
		
		BCStkRequestDAO dao = new BCStkRequestDAO();
		dao.restore(hql);
		
		String hql2 = "from BCReqConfirmBean where docNo = '"+docNo+"'";
//		hql2 = hql2 + " and billStatus=0 and isCancel =0 ";		
		BCReqConfirmDAO dao2 = new BCReqConfirmDAO();
		dao2.restore(hql2);
		
		request.setAttribute("docNoFind", docNo.substring(9, docNo.length()));
		
		return true;
	}

	@SuppressWarnings("unchecked")
	public BCStkRequestForm arrayListForm(ArrayList results) {
		BCStkRequestForm form = new BCStkRequestForm();
						
		
		if (results!=null) {
			
			for(int i=0; i<results.size(); i++) {				
				BCStkRequestBean s2 = (BCStkRequestBean)results.get(i);
								
				form.setDocNo(s2.getDocNo());
				form.setDocDate(""+s2.getDocDate());
				form.setDepartCode(s2.getDepartCode());
				form.setProjectCode(s2.getProjectCode());
				form.setCreatorCode(s2.getCreatorCode());
				form.setiTTL_deptRequest(s2.getIttl_deptRequest());
				form.setMyDescription(s2.getMyDescription());
																																
				int id = 0;
				
				for(Object o: s2.getBcStkRequestSubs()) {
					BCStkRequestSubBean sub2 = (BCStkRequestSubBean)o;
					id = sub2.getLineNumber();					
					switch (id) {
					case 1:						
						form.setItemCode1(sub2.getItemCode());
						form.setItemName1(sub2.getItemName());						
//						form.setUnitCode1(sub2.getBcitemUnit().getCode());
//						form.setUnitName1(sub2.getBcitemUnit().getName());
						form.setUnitCode1(sub2.getUnitCode());
						form.setUnitName1(sub2.getIttl_unitName());
						
						form.setQty1(""+sub2.getQty());						
						form.setDivision1(sub2.getIttl_division());						
						form.setIttl_unitName1(sub2.getIttl_unitName());
						form.setPrice1(""+sub2.getPrice());
						form.setAmount1(""+sub2.getAmount());
						break;
					case 2:
						form.setItemCode2(sub2.getItemCode());
						form.setItemName2(sub2.getItemName());						
//						form.setUnitCode2(sub2.getBcitemUnit().getCode());
//						form.setUnitName2(sub2.getBcitemUnit().getName());
						form.setUnitCode2(sub2.getUnitCode());
						form.setUnitName2(sub2.getIttl_unitName());
						
						form.setQty2(""+sub2.getQty());						
						form.setDivision2(sub2.getIttl_division());
						form.setIttl_unitName2(sub2.getIttl_unitName());
						form.setPrice2(""+sub2.getPrice());
						form.setAmount2(""+sub2.getAmount());						
						break;
					case 3:
						form.setItemCode3(sub2.getItemCode());
						form.setItemName3(sub2.getItemName());
//						form.setUnitCode3(sub2.getBcitemUnit().getCode());
//						form.setUnitName3(sub2.getBcitemUnit().getName());
						form.setUnitCode3(sub2.getUnitCode());
						form.setUnitName3(sub2.getIttl_unitName());	
						
						form.setQty3(""+sub2.getQty());						
						form.setDivision3(sub2.getIttl_division());
						form.setIttl_unitName3(sub2.getIttl_unitName());
						form.setPrice3(""+sub2.getPrice());
						form.setAmount3(""+sub2.getAmount());						
						break;		
					case 4:
						form.setItemCode4(sub2.getItemCode());
						form.setItemName4(sub2.getItemName());
//						form.setUnitCode4(sub2.getBcitemUnit().getCode());
//						form.setUnitName4(sub2.getBcitemUnit().getName());
						form.setUnitCode4(sub2.getUnitCode());
						form.setUnitName4(sub2.getIttl_unitName());
						
						form.setQty4(""+sub2.getQty());						
						form.setDivision4(sub2.getIttl_division());
						form.setIttl_unitName4(sub2.getIttl_unitName());
						form.setPrice4(""+sub2.getPrice());
						form.setAmount4(""+sub2.getAmount());						
						break;						
					}	
				}				
			}			
		}	
		return form;
	}

	public void showFind(HttpServletRequest request) throws UnsupportedEncodingException {
		String userName = (String)request.getSession().getAttribute("userName");			
		String monthFind = (String)request.getParameter("monthFind");
		String yearFind = (String)request.getParameter("yearFind");

//		String docNoFind = (String)request.getParameter("itemFind");
		@SuppressWarnings("unused")
		String preDocNo  = (String)request.getSession().getAttribute("preDocNo")+"-PR";
		@SuppressWarnings("unused")
		int userLevel =  (Integer)request.getSession().getAttribute("userLevel");		
		System.out.println("userLevel = "+userLevel);
		
		String	itemFind = "";
		if(request.getParameter("itemFind")!=null) {
			itemFind = ((String)request.getParameter("itemFind"));
			itemFind = new String(itemFind.getBytes("ISO8859_1"),"UTF-8");
			request.setAttribute("itemFind", itemFind.trim());
			//request.getSession().setAttribute("itemFind", docNoFind.trim());
		}		
		
		if(monthFind==null) {			
			monthFind = CommonUtils.getCurrentDateFormat("MM");										
		}
		
		if(yearFind==null) {
			yearFind = CommonUtils.getCurrentDateFormat("yyyyy");
			
			if (Integer.parseInt(yearFind)<2500) {								
				yearFind = (""+(Integer.parseInt(yearFind)+543)).substring(2);			
			}else {
				yearFind = yearFind.substring(2);
			}			
		}
				
		request.getSession().setAttribute("monthFind", monthFind);
		request.getSession().setAttribute("yearFind", yearFind);
		
		List<ITTL_UserDepartment> userInDepartmentList =  (List<ITTL_UserDepartment>)request.getSession().getAttribute("userInDepartmentList");
		String createorCode = "";
		if(userInDepartmentList!=null && userInDepartmentList.size()>0) {
			for (ITTL_UserDepartment userInDept : userInDepartmentList) {
				createorCode = createorCode+"'"+userInDept.getUserInDepartment()+"',";
			}
			createorCode = createorCode + "'"+userName+"'";
		}		
				
		String hql = "";
		
		if(itemFind==null||itemFind.trim().equals("")) {
			hql = "FROM BCStkRequestBean s  ";
			if(userLevel<9) {
				hql += "WHERE upper(s.creatorCode)='"+userName.toUpperCase()+"' ";
			}
//					 "and s.docNo like '"+preDocNo+"-"+yearFind+monthFind+"%' "+					 			
		}else {
			hql = "FROM BCStkRequestBean s  ";
			if(userLevel<9) {
				hql += "WHERE upper(s.creatorCode)='"+userName.toUpperCase()+"' "+
					 "and s.docNo like '%"+itemFind.trim()+"%' ";			
//					 "and s.docNo like '"+preDocNo+"-%"+docNoFind.trim()+"%' "+
			}else {
				hql += "WHERE  "+
						 " s.docNo like '%"+itemFind.trim()+"%' ";							
			}
		}
		hql += "ORDER BY s.docNo desc";
		
		hql = "FROM BCStkRequestBean WHERE docNo like '%"+itemFind.trim()+"%' ORDER BY docNo desc";
// ---------------- start
		if(itemFind==null||itemFind.trim().equals("")) {	
			hql = "FROM BCStkRequestBean s WHERE s.creatorCode in ("+createorCode+") ";
		}else {
			
			hql = "FROM BCStkRequestBean s WHERE s.creatorCode in ("+createorCode+") " +
					" and s.docNo like '%"+itemFind.trim()+"%' " ;
		}	
		
		 preDocNo  = (String)request.getParameter("deptFind");
		request.setAttribute("deptFind", preDocNo);
		
		String dateFind = "";
		if(request.getParameter("dateFind")!=null) {
			dateFind = ((String)request.getParameter("dateFind"));
			dateFind = new String(dateFind.getBytes("ISO8859_1"),"UTF-8");
			request.setAttribute("dateFind", dateFind.trim());
		}		
		String ittl_status = (String)request.getParameter("ittl_status2");
		request.setAttribute("ittl_status2", ittl_status);
		System.out.println("ittl_status="+ittl_status);	
		
		
		if(preDocNo!=null && !preDocNo.equals("")) {
			hql += " and docNo like '"+preDocNo+"%' ";
		}		
		if(dateFind!=null && !dateFind.trim().equals("")) {
			hql += " and docNo like '%"+dateFind+"%' ";
		}
		if(ittl_status!=null && !ittl_status.equals("all") && !ittl_status.equals("")) {
			hql += " and ittl_status = '"+ittl_status+"' ";
		}		
		hql +=" ORDER BY docNo";
// ----------------- end		
		
		System.out.println(hql);
		Page.setPage(request, hql);
		
	}

	public boolean updateForApprove(ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		
		BCStkRequestForm reqForm = setForm(request);				
		BCStkRequestBean  stkReq = setRequestObject(reqForm);		
		stkReq.setIttl_status("3");
		stkReq.setIttl_userLevel((Integer)request.getSession().getAttribute("userLevel"));
		
		if(stkReq.getBcStkRequestSubs()!=null && stkReq.getBcStkRequestSubs().size()>0) {
			Iterator it = stkReq.getBcStkRequestSubs().iterator();
			while (it.hasNext()) {
				BCStkRequestSubBean sub = (BCStkRequestSubBean) it.next();
				sub.setIttl_status("3");
			}
		}
		
		BCStkRequestDAO dao = new BCStkRequestDAO();
		dao.update(stkReq.getDocNo(),stkReq);
		
		String user 	 = (String)request.getSession().getAttribute("userName");
		System.out.println(CommonUtils.getDateTimeForStamp()+" #user:"+user+" #use :BCStkRequestDAO #mode :updateForApprove");
		
		BCReqConfirmBean reqComfirm = setReqConfirm(stkReq);						
		reqComfirm.setIttl_status("3");
		reqComfirm.setIttl_userLevel((Integer)request.getSession().getAttribute("userLevel"));
		
		if(reqComfirm.getBcReqConfirmSubs()!=null && reqComfirm.getBcReqConfirmSubs().size()>0) {
			Iterator it = reqComfirm.getBcReqConfirmSubs().iterator();
			while (it.hasNext()) {
				BCReqConfirmSubBean req = (BCReqConfirmSubBean) it.next();
				req.setIttl_status("3");
			}
		}
		
		BCReqConfirmDAO dao2 = new BCReqConfirmDAO();
		dao2.update(reqComfirm.getDocNo(), reqComfirm);
				
		System.out.println(CommonUtils.getDateTimeForStamp()+" #user:"+user+" #use :BCReqConfirmDAO #mode :update");	
		
		//request.getSession().setAttribute("itemFind", reqForm.getDocNo().substring(9, reqForm.getDocNo().length()));
		request.getSession().setAttribute("itemFind", reqForm.getDocNo());
		return true;
	}
}
